package gui;

import java.util.ArrayList;
import java.util.concurrent.CountDownLatch;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.MessageBox;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.widgets.Text;

import details.Cd;
import details.Track;
import details.User;

public class AddAlbumDialog extends Dialog {
	private static Display display;
	private static AddAlbumDialog inst;
	private static Shell shell;
	private static Shell dialogShell;
	private static Button logDialogButtonExit;
	private static Button logDialogButtonStart;
	private static Button addTrackButton;
	private static Button deleteTrackButton;
	private static Group logDialogGroup;
	private static Text albumTitleText;
	private static Text albumPerformerText;
	private static Text albumGenreText;
	private static Text albumYearText;
	private static Text albumLengthText;
	public static boolean successful = false;
	private static Table albumTable;
	private static String[] columnNames = {"Title", "Artist", "Length"};
	private ArrayList<Track> tracks = new ArrayList<Track>();
	private static Track track;
	private static Button variuos;

	static Image background = new Image(display,
			Main.class.getResourceAsStream("addEdit-back.png"));

	public static void openLogDialog() {
		try {
			display = Display.getDefault();
			shell = new Shell(display);
			shell.setLocation(Main.getCentral_x() - 406,
					Main.getCentral_y() - 458);
			shell.setSize(900, 800);
			shell.setBackgroundImage(background);
			inst = new AddAlbumDialog(shell, 0);
			inst.open();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void closeInitDialog() {
		shell.close();
	}

	public AddAlbumDialog(Shell parent, int style) {
		super(parent, style);
	}

	public void open() {
		try {
			Shell parent = getParent();
			dialogShell = new Shell(parent, 67680);

			dialogShell.setText("Music Box - Add Album");
			dialogShell.setBackgroundImage(background);
			dialogShell.setLayout(null);

			logDialogGroup = new Group(dialogShell, 0);
			logDialogGroup.setLayout(null);
			logDialogGroup.setBackgroundImage(background);
			logDialogGroup.setBounds(0, -8, 550, 594);

			albumTitleText = new Text(logDialogGroup, 2048);
			albumTitleText.setBounds(280, 70, 140, 28);

			albumGenreText = new Text(logDialogGroup, 2048);
			albumGenreText.setBounds(280, 115, 140, 28);

			variuos = new Button(logDialogGroup, SWT.CHECK);
			variuos.setBounds(340, 165, 10, 12);
			
			albumPerformerText = new Text(logDialogGroup, 2048);
			albumPerformerText.setBounds(280, 200, 140, 28);
			
			albumYearText = new Text(logDialogGroup, 2048);
			albumYearText.setBounds(280, 245, 140, 28);


			albumTable = new Table(logDialogGroup, SWT.SINGLE | SWT.BORDER | SWT.FULL_SELECTION);
			albumTable.setHeaderVisible(true);	
			
			albumTable.setBounds(57, 328, 450, 220);
			//albumtable.setMenu(createPopUpMenu());	
			albumTable.addSelectionListener(new SelectionAdapter() {
				public void widgetDefaultSelected(SelectionEvent e) {
					TableItem[] items = albumTable.getSelection();
					if (items.length > 0) editEntry(items[0]); //get details
				}
			});
				TableColumn column1 = new TableColumn(albumTable, SWT.NONE);
				column1.setText("Title");
				column1.setWidth(190);
				TableColumn column2 = new TableColumn(albumTable, SWT.NONE);
				column2.setText("Artist");
				column2.setWidth(170);
				TableColumn column3 = new TableColumn(albumTable, SWT.NONE);
				column3.setText("length");
				column3.setWidth(80);
				
			
			
			logDialogButtonStart = new Button(logDialogGroup, 16777224);

			logDialogButtonStart.setText("Add Album");
			logDialogButtonStart.setBounds(130, 555, 110, 30);
			logDialogButtonStart.addListener(13, new Listener() {
				public void handleEvent(Event event) {
					String title = AddAlbumDialog.albumTitleText.getText();
					String genre = AddAlbumDialog.albumGenreText.getText();
					String performer = AddAlbumDialog.albumPerformerText.getText();
					String year = AddAlbumDialog.albumYearText.getText();

					if (title == "") {
						MessageBox messageBox = new MessageBox(
								AddAlbumDialog.shell, 296);

						messageBox.setMessage("Enter Title");
						messageBox.open();
					}
					if (genre == "") {
						MessageBox messageBox = new MessageBox(
								AddAlbumDialog.shell, 296);

						messageBox.setMessage("Enter Genre");
						messageBox.open();
					}
					if (performer == "") {
						MessageBox messageBox = new MessageBox(
								AddAlbumDialog.shell, 296);

						messageBox.setMessage("EnterPerformer");
						messageBox.open();
					}
					if (year == "") {
						MessageBox messageBox = new MessageBox(
								AddAlbumDialog.shell, 296);

						messageBox.setMessage("Enter Year");
						messageBox.open();
					}
					if (albumTable.getItemCount()==0) {
						MessageBox messageBox = new MessageBox(
								AddAlbumDialog.shell, 296);

						messageBox.setMessage("You Must Enter At Least One Track");
						messageBox.open();
					} 
					
					else {
						
						for (int i = 0; i < albumTable.getItemCount(); i++) {
							TableItem data = albumTable.getItem(i);
							track = new Track(null, i+1, data.getText(0),
									data.getText(1), Integer.parseInt(data.getText(2)), 0);
							tracks.add(track);
						}
						System.out.println(tracks.size());
						if (variuos.getSelection()){
							performer = "Various";
						}
						Main.signal = new CountDownLatch(1);
						
						
						Main.dbGui.addCd(title, genre, performer, Integer.parseInt(year), tracks);
						try {
							Main.signal.await();
						} catch (InterruptedException e) {
							MessageBox messageBox = new MessageBox(
									AddAlbumDialog.shell, 32);
							messageBox.setText("Error");
							messageBox.setMessage("Error: "
									+ e.getLocalizedMessage());
							messageBox.open();
							
							e.printStackTrace();
						}

						String guiErrorMsg = Main.guiInt.getMassage();
						System.out.println(guiErrorMsg + "login class");
						if (guiErrorMsg!=null){
							MessageBox messageBox = new MessageBox(
									AddAlbumDialog.shell, 32);
							messageBox.setText("Error");
							messageBox.setMessage("Error: "
									+ guiErrorMsg);
							messageBox.open();
						} else{
							
							MessageBox messageBox = new MessageBox(
									AddAlbumDialog.shell, 32);
							messageBox.setText("Add Album");
							messageBox
									.setMessage("Album added seccessfuly");
							messageBox.open();
							AddAlbumDialog.successful = true;
							AddAlbumDialog.shell.close();
						}
					}
				}
			});
			addTrackButton = new Button(logDialogGroup, 16777224);

			addTrackButton.setText("Add track");
			addTrackButton.setBounds(215, 285, 110, 30);
			addTrackButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					newEntry();
				
				}
			});
			
			deleteTrackButton = new Button(logDialogGroup, 16777224);

			deleteTrackButton.setText("Remove track");
			deleteTrackButton.setBounds(335, 285, 110, 30);
			deleteTrackButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent e) {
					TableItem[] items = albumTable.getSelection();
					if (items.length == 0) return;
					items[0].dispose();
					}
			});
			
			
			logDialogButtonExit = new Button(logDialogGroup, 16777224);

			logDialogButtonExit.setText("Cancel");
			logDialogButtonExit.setBounds(300, 555, 110, 30);
			logDialogButtonExit.addListener(13, new Listener() {
				public void handleEvent(Event event) {
					AddAlbumDialog.shell.close();
				}
			});
			dialogShell.layout();
			dialogShell.pack();
			dialogShell.setLocation(getParent().toDisplay(100, 100));
			dialogShell.open();
			Display display = dialogShell.getDisplay();

			while (!dialogShell.isDisposed())
				if (!display.readAndDispatch())
					display.sleep();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static Shell getDialogShell() {
		return dialogShell;
	}

	public static void setDialogShell(Shell dialogShell) {
		dialogShell = dialogShell;
	}

	public static Button getInitDialogButtonExit() {
		return logDialogButtonExit;
	}

	public static void setInitDialogButtonExit(Button logDialogButtonExit) {
		logDialogButtonExit = logDialogButtonExit;
	}

	public static Button getInitDialogButtonStart() {
		return logDialogButtonStart;
	}

	public static void setInitDialogButtonStart(Button logDialogButtonStart) {
		logDialogButtonStart = logDialogButtonStart;
	}

	public static Group getInitDialogGroup() {
		return logDialogGroup;
	}

	public static void setInitDialogGroup(Group logDialogGroup) {
		logDialogGroup = logDialogGroup;
	}

	public static Display getInitDisplay() {
		return display;
	}

	public static void setDisplay(Display display) {
		display = display;
	}

	public static Display getDisplay() {
		return display;
	}

	public static AddAlbumDialog getInst() {
		return inst;
	}

	public static Shell getShell() {
		return shell;
	}
	
	private static void editEntry(TableItem item) {
		DataEntryDialog dialog = new DataEntryDialog(dialogShell);
		dialog.setLabels(columnNames);
		String[] values = new String[3];
		for (int i = 0; i < values.length; i++) {
			values[i] = item.getText(i);
		}
		dialog.setValues(values);
		values = dialog.open();
		if (values != null){
			item.setText(values);
		}
		
	}
	
	private void newEntry() {
		DataEntryDialog dialog = new DataEntryDialog(dialogShell);
		dialog.setLabels(columnNames);
		String[] data = dialog.open();
		if (data != null) {
			TableItem item = new TableItem(albumTable, SWT.NONE);
			item.setText(data);
		}
	}
}